package com.hqj.client.controller;

import com.hqj.client.entity.BlogsLikes;
import com.hqj.client.service.BlogsLikesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王建林
 * @version 1.0
 * @date 2020/10/15
 */
@Api(tags = "博客点赞")
@Controller
@RequestMapping("/BlogsLikesController")
public class BlogsLikesController {

    @Resource
    private BlogsLikesService blogsLikesService;

    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public String findPage(int page, int size, Model model) {
        List<BlogsLikes> page1 = blogsLikesService.findPage(page, size);
        model.addAttribute("cx", page1);
        return "/page/BlogIndex";
    }


    @ApiOperation("增加")
    @PostMapping("/add")
    public String add(@RequestBody BlogsLikes blogsLikes, Model model) {
        blogsLikesService.add(blogsLikes);
        model.addAttribute("zj", "新增点赞");
        return "/page/BlogIndex";
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public String updates(@RequestBody BlogsLikes blogsLikes, Model model) {
        int i = blogsLikesService.updates(blogsLikes);
        if (i > 0) {
            model.addAttribute("xg", "修改成功");
        }
        return "/page/BlogIndex";
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public String delete(@RequestParam Long id,Model model) {
        int i = blogsLikesService.delete(id);
        if (i > 0) {
            model.addAttribute("sc","删除成功");
        }
        return "/page/BlogIndex";
    }
    @ApiOperation("判断是否点赞")
    @GetMapping("/likes")
    public String likes(@RequestParam Long blogsId,
                             @RequestParam String usernamem,Model model){
        BlogsLikes blogsLikes = blogsLikesService.judgelikes(blogsId,usernamem);
        if (blogsLikes!=null){
            model.addAttribute("msg",blogsLikes);
            return "/page/BlogIndex";
        }
        return"/page/BlogIndex";
    }


}
